package com.twu.biblioteca;

public class MenuAlertListener implements Listener {

    private MenuAlert alert;
    private Catalogue catalogue;
    private int MIN_OPTION = 1;

    public MenuAlertListener(Catalogue catalogue, MenuAlert alert){
        this.catalogue = catalogue;
        this.alert = alert;
    }

    @Override
    public void notifyListener(int action) throws Exception {
        switch (action)
        {
            case 1:
                System.out.println("\n\t(Listing "+catalogue.bookCount()+" book(s) available)");
                System.out.println(catalogue.listBooks());
                returnToMainMenu();
                break;

            case 2:
                System.out.println("\n\t(Listing "+catalogue.movieCount()+" movie(s) available)");
                System.out.println(catalogue.listMovies());
                returnToMainMenu();
                break;

            case 3:
                System.out.println("\n\t(Choose from "+catalogue.bookCount()+" book(s) available)");
                System.out.println(catalogue.listBooks());
                requestNextAction(action);
                break;

            case 4:
                System.out.println("\n\t(Showing membership status)");
                System.out.println(new Membership().checkStatus());
                requestNextAction(action);
                break;
        }
    }

    private void requestNextAction(int previous_action) throws Exception {
        System.out.println("\n\t(Press <Enter Key> to return to main menu)");
        if(alert.getConsoleInput().matches("")){
           returnToMainMenu();
           this.notifyListener(alert.getUserOption());
        }
        else if(previous_action ==3) {
            System.out.println(new Reservation().addBook(catalogue.available_books.get(alert.getUserOption() - MIN_OPTION)));
            returnToMainMenu();
        }
    }
    public void returnToMainMenu() throws Exception {
         System.out.println(alert.showMenuOptions());
         this.notifyListener(alert.getUserOption());
    }

}
